package com;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author fuguangli
 * @description 前沿类
 * @Create date:    2017/6/27
 */
public class Tgd {

    private static AtomicInteger count = new AtomicInteger(1);
    private static int countInt = 100;
    private static int countCA = 0;
    private static int countCB = 1;
    private static volatile Integer t = 1;
    private static Lock lock = new ReentrantLock();
    private static Condition conditionA = lock.newCondition();
    private static Condition conditionB = lock.newCondition();
    private static ThreadLocal threadLocal = new ThreadLocal();


    static class Resource {
        public void hello() {
            System.err.println("hello");
        }
    }

    static class WorkA {
        private Resource resource = new Resource();

        public synchronized void smethod1() {
            resource.hello();
            try {
                Thread.sleep(12000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void method2() {
            lock.lock();
            System.err.println("lock.method2");
            lock.unlock();

        }

        public void method3() {
            lock.lock();
            System.err.println("lock.method3");


        }

        public void method4() {
            lock.lock();

            System.err.println("lock.method4");
            try {

                conditionA.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        public void method5() {
            lock.lock();

            System.err.println("lock.method5");

            try {

                conditionB.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    static class WorkB {
        public void method1() {
            for (int i = 0; i < 1000; i++) {
                countCA = count.addAndGet(1);
                System.out.println("method1-------count=" + count);
            }
        }

        public void method2() {
            for (int i = 0; i < 1000; i++) {
                countCB = count.addAndGet(1);
                System.out.println("method2-------count=" + count);
            }
        }

        public void method3() {
            for (int i = 0; i < 1000000; i++) {
                countCA = ++countInt;
                System.out.println("method3-------count=" + countInt);
            }
        }

        public void method4() {
            for (int i = 0; i < 1000000; i++) {
                countCB = ++countInt;
                System.out.println("method4-------count=" + countInt);
            }
        }

        public void method5() {
            while (countInt > 0) {
                countInt--;
                System.out.println("method5-------count=" + countInt);

            }

        }

        public void method6() {
            while (countInt > 0) {
                countInt--;
                System.out.println("method6-------count=" + countInt);

            }
        }

        public void methodC() {
            while (true) {
                if (countCA == countCB) {
                    System.err.println("warning:线程资源混乱");
                }
            }
        }

    }


    public static void main(String[] args) {
        Tgd tgd = new Tgd();
        WorkA workA = new WorkA();
        WorkB workB = new WorkB();

        int targetF = 0;
        Set<Integer> set = new HashSet<>();


        ExecutorService executorService = Executors.newFixedThreadPool(10000);

        for (int i = 0; i < 100000; i++) {
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    while (countInt > 0) {
//                        try {
//                            Thread.sleep(500);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                        countInt--;
//                        count.decrementAndGet();
//                        System.out.println("current count=" + count);
//                        System.out.println(Thread.currentThread().getName());
                    }
                    if(countInt<0){
                        System.err.println(Thread.currentThread().getName()+" ERROR!");
                    }
//                    System.err.println(Thread.currentThread().getName()+" FINISH!");
                }
            });
        }
        executorService.shutdown();
    }
}
